package com.lq.schoolshop.controller.except;

import com.lq.schoolshop.bean.Result;
import com.lq.schoolshop.bean.StatusCode;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

/**
 * @Author
 * @Date2021/1/11 21:42
 * @Version V1.0
 *  异常捕获类
 **/
@CrossOrigin(origins = "*",maxAge = 3600)
@ControllerAdvice
public class BaseExceptionHandler {

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result error(Exception e, HttpServletResponse response) {
        //解决跨域
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST,OPTIONS,GET");
        System.out.println("进入了这个异常："+e.getMessage());
        return new Result(false, StatusCode.PROGRAMEError, "服务器繁忙,请稍后再试吧。Code:"+e.getMessage());
    }


}
